package com.umpay.junit.test.h5.wx;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.springframework.test.util.AssertionErrors.fail;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.ModelAndView;

import com.umfwechat.h5.controller.TestController;
import com.umfwechat.h5.controller.sign.SignController;
import com.umfwechat.util.PropertyUtil;
import com.umfwechat.wx.controller.WeChatController;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-context.xml" })
public class WeChatTestController {

	@Resource
	TestController ttestController;

	@Resource
	WeChatController weChatController;

	private MockHttpServletRequest request;
	private MockHttpServletResponse response;

	static {
		System.setProperty("resin.home","E:\\developSoft\\resin\\resin-4.0.48"); 
		//String url = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","junit.test.url", ""); 
    	//System.setProperty("resin.home", url);
	}

	@Before
	public void before() {
		/**
		 * 设置resin_home环境变量
		 */
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		request.setCharacterEncoding("UTF-8");
	}

	@Test
	public void test() {
		// request.setRequestURI("createProduct.htm");
		// request.setMethod(HttpMethod.POST.name());

		ModelAndView mv = null;
		try {
			mv = ttestController.test(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}

		assertNotNull(mv);
		assertEquals(response.getStatus(), 200);
	}
	
	
	/**
	 * 邀请收银员、收款人
	 * @return
	 */
	@Test
	public void getDeveloper() {

		try {
			String sceneId="2000001132";
			String fromUserName="110";
			String res = weChatController.scanQrcode(sceneId, fromUserName).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}

	/**
	 * 获取AccessToken
	 * @return
	 */
	@Test
	public void getAccessToken() {

		try {
			String ss="fiRUhgOtFxhQgv6XVtXPG8V8UrY5z3cVgnLL649PG48u9NXuRzcpCZ-UBs0rpNYocmnzzel4WXqBJejcLKPrPQUK9LGNfLAQdBrQ_djsx7EjYXgFPdu6IUZGj6jpZ99SGRVeABAYJN";
			String appid="wx1fc6210c11928102";
			String secret="ea1b2620f0ac844cffc747fac41adda7";
			String res = weChatController.getAccessToken(appid, secret)
					.toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}

	/**
	 * 创建个性化菜单
	 */
	@Test
	public void createMenu() {
		try {
			String res = weChatController.createMenu().toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
	
	
	/**
	 * 生成临时二维码
	 * @return
	 */
	@Test
	public void createTemporaryORCode() {
		try {
			Integer scene_id=221221221;
			String expireSeconds="600";
			String res = weChatController.createTemporaryQrCode(expireSeconds,scene_id).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}

	/**
	 * 给用户打标签
	 */
	@Test
	public void addUserGroup() {
		try {
			String openid="o9Gdj1Z89rbDTqeWZN5NCCbPR00c";
			Integer to_groupid=0;
			String res = weChatController.addUserGroup(openid, to_groupid).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
	
	/**
	 * 添加客服帐号
	 */
	@Test
	public void addCustomer() {
		try {
			String kf_account="test1@test";
			String nickname="客服1";
			String password="pswmd5";
			String res = weChatController.addCustomer(kf_account, nickname, password).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
	
	/**
	 * 发送客服消息
	 */
	@Test
	public void sendMessage() {
		try {
			String openid="o9Gdj1Z89rbDTqeWZN5NCCbPR00c";
			String text="卡阿卡卡卡卡卡卡卡卡卡卡";
			String res = weChatController.sendMessage(text, openid).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
	
	
	/**
	 * 获得模板ID
	 */
	@Test
	public void getIndustry() {
		try {
			
			String res = weChatController.getIndustry().toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
	
	/**
	 * 发送模板消息
	 */
	@Test
	public void sendTemplateMessage() {
		try {
			String openid="o9Gdj1Z89rbDTqeWZN5NCCbPR00c";
			String template_id="nEAOn3kiKTCvJK6f322GQtx6tM84HF68dngLx9GWxSg";
			String url="localhost:8080/umfWeChatPlat/sen";
			String amount="100";
			String payTime="201010";
			String payType="zhifu";
			Map map=new HashMap();
			String res = weChatController.sendTemplateMessage(map).toString();
			System.out.println("res=" + res);
		} catch (Exception e) {
			e.printStackTrace();
			fail("testToSearchPage failed!");
		}
		assertEquals(response.getStatus(), 200);
	}
}
